﻿using HlELock.Core;
using HlELock.Core.Events;
using Prism.Commands;
using Prism.Mvvm;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace HlELock.Page.ViewModels
{
    public class SearchHotelDeviceControlViewModel : BindableBase
    {
        public SearchHotelDeviceControlViewModel()
        {

            Task.Run(SearchDevice);
            Descr = AppHelper.GetResourceString("ZhenZaiSouSuoSheBei");
        }

        private DelegateCommand _activeCommand;
        public DelegateCommand ActiveCommand =>
            _activeCommand ?? (_activeCommand = new DelegateCommand(async () => await ExecuteActiveCommand()));

        /// <summary>
        /// 激活设备
        /// </summary>
        async Task ExecuteActiveCommand()
        {
            EventBus.StartDialogLoading(AppHelper.GetResourceString("ZhenZaiJiHuo"));
            //从后台获取激活码
            var result = await HttpHelper.GetHotelDeviceActiveCodeAsync();
            if (!result.IsActive)
            {
                MessageBox.Show(AppHelper.GetResourceString("DiQuCuoWu"));
                return;
            }

            MyAppContext.Device.ActiveHotelDevice(result.AreaCode, result.AgencyCode, result.HotelCode);
            EventBus.StopDialogLoading();
            Descr = AppHelper.GetResourceString("JiHuoWanCheng");
            ShowActivated = Visibility.Hidden;
        }

        private string _descr;
        public string Descr
        {
            get { return _descr; }
            set { SetProperty(ref _descr, value); }
        }
        private Visibility _showBusy;
        public Visibility ShowBusy
        {
            get { return _showBusy; }
            set { SetProperty(ref _showBusy, value); }
        }

        private Visibility _showActivated = Visibility.Collapsed;
        public Visibility ShowActivated
        {
            get { return _showActivated; }
            set { SetProperty(ref _showActivated, value); }
        }


        private void SearchDevice()
        {
            var lst = UsbHidHelper.GetDeviceList();
            var usb = lst.FirstOrDefault(f => f.VID == MyAppContext.VID && f.PID == MyAppContext.PID);

            if (usb == null)
            {
                Descr = AppHelper.GetResourceString("MeiYouZhaoDaoSheBei");
            }
            else
            {
                MyAppContext.Device = usb;
                Descr = AppHelper.GetResourceString("LianJieSheBei");
                if (usb.Open())
                {
                    Descr = AppHelper.GetResourceString("JianCeSheBei");
                    MyAppContext.Device.GetHotelAgenyCode(agencyCode =>
                    {
                        if (agencyCode == "0000" || agencyCode == "1234")
                        {
                            ShowActivated = Visibility.Visible;
                            Descr = AppHelper.GetResourceString("SheBeiHaiMeiYouJiHuo");

                        }
                        else if (agencyCode != MyAppContext.HotelInfo.AgencyCode)
                        {
                            Descr = AppHelper.GetResourceString("SheBeiWenTi");
                            return;
                        }
                        else
                        {
                            MyAppContext.Device.GetHotelCode(hotelCode =>
                            {
                                if (hotelCode != MyAppContext.HotelInfo.HotelCode)
                                {
                                    ShowActivated = Visibility.Visible;
                                    Descr = AppHelper.GetResourceString("SheBeiHaiMeiYouJiHuo");
                                }
                                else
                                {
                                    EventBus.CloseDialog();
                                }
                                ShowBusy = Visibility.Collapsed;
                            });
                        }

                    });
                }
                else
                {
                    Descr = AppHelper.GetResourceString("LianJieShiBai");
                }
            }
        }
    }
}
